package com.echoesnet.echoesshortvideo.Activities;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.echoesnet.echoesshortvideo.Beans.IncludeNavBarBean;
import com.echoesnet.echoesshortvideo.R;
import com.echoesnet.echoesshortvideo.Utils.ToastUtils;
import com.echoesnet.echoesshortvideo.View.EditViewWithCharIndicate;
import com.echoesnet.echoesshortvideo.ViewModels.SettingViewModel;
import com.echoesnet.echoesshortvideo.databinding.ActivityFeedBackBinding;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class FeedBackActivity extends BaseActivity
{

    @BindView(R.id.evw_input_feedback)
    EditViewWithCharIndicate evwInputFeedback;
    private Unbinder unbinder;
    private SettingViewModel settingViewModel;
    private ActivityFeedBackBinding actBinding;
    private IncludeNavBarBean navBean;
    private TextView  tvCommit;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        actBinding = DataBindingUtil.setContentView(this, R.layout.activity_feed_back);
        navBean = new IncludeNavBarBean("反馈与帮助", true);
        navBean.setShowRight(true);
        navBean.setTextRight("提交");
        actBinding.relNavBar.setNavBean(navBean);
        actBinding.relNavBar.itvLeft.setOnClickListener(view -> finish());

        tvCommit = actBinding.relNavBar.itvRight;
        tvCommit.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (TextUtils.isEmpty(evwInputFeedback.getInputText().trim()))
                    ToastUtils.showShort("意见不能为空");
                else
                    settingViewModel.postFeedBack("1", evwInputFeedback.getInputText());
            }
        });

        unbinder = ButterKnife.bind(this);
        initView();
    }

    private void initView()
    {
        settingViewModel = ViewModelProviders.of(this).get(SettingViewModel.class);
        settingViewModel.getFeedBack().observe(this, new Observer<String>()
        {
            @Override
            public void onChanged(@Nullable String s)
            {
                ToastUtils.showShort("提交反馈成功！感谢您的参与。");
                finish();
            }
        });
        if (evwInputFeedback.getInputText().isEmpty())
        {
            tvCommit.setClickable(false);
            tvCommit.setTextColor(ContextCompat.getColor(FeedBackActivity.this, R.color.C0331));
        }else
        {
            tvCommit.setClickable(true);
            tvCommit.setTextColor(ContextCompat.getColor(FeedBackActivity.this, R.color.white));
        }
        evwInputFeedback.getEditTextView().addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {

            }

            @Override
            public void afterTextChanged(Editable s)
            {

                if (evwInputFeedback.getInputText().isEmpty())
                {
                    tvCommit.setClickable(false);
                    tvCommit.setTextColor(ContextCompat.getColor(FeedBackActivity.this, R.color.C0331));
                }else
                {
                    tvCommit.setClickable(true);
                    tvCommit.setTextColor(ContextCompat.getColor(FeedBackActivity.this, R.color.white));
                }
            }
        });



    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if (unbinder != null)
            unbinder.unbind();
    }

    @OnClick({ R.id.evw_input_feedback})
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.evw_input_feedback:
                break;
        }
    }
}
